package com.example.myapplication;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.myapplication.databinding.ActivityMainBinding;
import com.google.gson.Gson;



import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        Log.d("tag","create");

        binding.get.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                OkHttpClient client = new OkHttpClient();

                Request request =  new Request.Builder()
                        .url("http://192.168.10.246:18183/api/currentTime")
                        .build();

                client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        Log.e("err",e.getMessage());
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        if(response.isSuccessful()){
                            String res = response.body().string();
                            Gson gson = new Gson();

                            apiData apidata = gson.fromJson(res,apiData.class);

                            Log.d("msg", apidata.getData().getShow());


                        }else {
                            Toast.makeText(MainActivity.this, "请求失败", Toast.LENGTH_SHORT).show();
                        }

                    }
                });
            }
        });

        binding.post.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //  添加参数
//                FormBody.Builder builder = new FormBody.Builder();
//                builder.add("username","liuren");
//                builder.add("password","IrIBYUBsu6");

                OkHttpClient clientH = new OkHttpClient();
                //json数据
                MediaType JSON = MediaType.parse("application/json; charset=utf-8");
                // Json数据
                String jsonStr = "{\"username\":\"liuren\",\"password\":\"IrIBYUBsu6\"}";
                // 得到RequestBody对象
                RequestBody body = RequestBody.create(JSON, jsonStr);


                //创建一个Request对象
                Request request = new Request.Builder()
                        .url("http://192.168.10.246:18183/api/user/login")
                        .post(body)
                        .build();

                clientH.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(@NonNull Call call, @NonNull IOException e) {
                        Log.d("post","失败");
                    }

                    @Override
                    public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                        if(response.isSuccessful()){
                            String res = response.body().string();
                            Gson gson = new Gson();
                            loginData apidataPost = gson.fromJson(res,loginData.class);
                            Log.d("postMsg",apidataPost.getData().getUser().getNickname());
                        }else {
                            Log.e("postErr","失败了");
                        }
                    }
                });
            }
        });
    }
}
